package com.it2297.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;

import com.it2297.component.AbstractPagination;
import com.it2297.model.Bid;
import com.it2297.model.UsedItem;
import com.it2297.view.BiddersBrowseView;
import com.it2297.view.BiddersDetailDialog;

public class BiddersController
{
	private final BiddersBrowseView browseView;
	private final BiddersDetailDialog detailDialog;
	private UsedItem selectedItem;
	private Bid selectedBid;

	public BiddersController(BiddersBrowseView browseView, BiddersDetailDialog detailDialog)
	{
		this.browseView = browseView;
		this.detailDialog = detailDialog;

		browseView.setPagination(new BrowseViewPagination());
		browseView.addTableMouseListener(new BrowseViewTableMouseListener());

		detailDialog.addSelectActionListener(new DetailDialogSelectActionListener());
		detailDialog.addCloseActionListener(new DetailDialogCloseActionListener());
	}

	public void showDetailDialog()
	{
		detailDialog.clearList();
		for (Bid bid : Bid.getMultiple(selectedItem.getId()))
		{
			detailDialog.addListElement(bid);
		}
		detailDialog.setVisible(true);
	}

	@SuppressWarnings("serial")
	private class BrowseViewPagination extends AbstractPagination
	{

		public BrowseViewPagination()
		{
			this.setCount(10);
			this.setTotal(Bid.getTotalUniqueUsedItem());
		}

		@Override
		protected void action(Object[] data)
		{
			// clear all row
			browseView.clearTableRow();

			// add new row base on data size
			for (int i = 0; i < data.length; i++)
			{
				browseView.addTableRow(new Object[] { data[i], data[i], data[i] });

			}
		}

		@Override
		protected Object[] subData(int position, int count)
		{

			return Bid.getMultipleUniqueBidItems(position, count).toArray();

		}
	}

	private class BrowseViewTableMouseListener extends MouseAdapter
	{

		@Override
		public void mouseClicked(MouseEvent mouseEvent)
		{
			selectedItem = (UsedItem) browseView.getSelectedItem();

			if (mouseEvent.getClickCount() > 1)
			{
				showDetailDialog();
			}
		}
	}

	private class DetailDialogSelectActionListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e)
		{
			if (!detailDialog.isListSelectionEmpty())
			{
				selectedBid = (Bid) detailDialog.getSelectedBid();

				//TODO BAD DESIGN, should do all transaction in updateWonBid()
				if (selectedBid.updateWonBid() && selectedBid.getUsedItem().updateCloseBid() && selectedBid.getUsedItem().updateWinner(selectedBid.getNeedy().getNric()))
				{
					JOptionPane.showMessageDialog(null, "Update successfully. Bid closed.");
					detailDialog.setVisible(false);
				}
				else
				{
					JOptionPane.showMessageDialog(null, "There's some error in the transaction...");
				}
			}
		}

	}

	public class DetailDialogCloseActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			detailDialog.setVisible(false);
		}
	}

}
